﻿#include "B3HsmsHostSim.h"
#include <QDateTime>

B3HsmsHostSim::B3HsmsHostSim(QObject *parent)
    :HsmsHost (parent)
{
    QObject::connect(this, &HsmsDevice::dataReady, this, &B3HsmsHostSim::privateDataMessage);
    registerDataMessage(6, 11); //ControlStateChangeOnLineLocal
    registerDataMessage(1, 6); //EquipmentStatusData
    registerDataMessage(6, 209);//OperatorAuthorityRequest
    registerDataMessage(2, 210);//OperatorAuthorityDownloadReply
    registerDataMessage(6, 207);//PanelInfoRequest
    registerDataMessage(2, 208);//PanelInfoDownloadReply
    registerDataMessage(6, 3);//EqpDiscreteVariableDataSendSites
    registerDataMessage(10, 6);//TerminalDisplayMultiBlockAcknowledge

    m_OnLineLocal = false;
}

void B3HsmsHostSim::privateDataMessage(const HSMSMessages &message)
{
    uint8_t stream = message.stream();
    uint8_t function = message.function();
    if(stream == 6 && function == 11) //ControlStateChangeOnLineLocal
    {
        onReceiveS6F11(message);
        return;
    }
    if(stream == 6 && function == 209) //OperatorAuthorityRequest
    {
        onReceiveS6F209(message);
        return;
    }
    if(stream == 2 && function == 210) //OperatorAuthorityDownloadReply
    {
        onReceiveS2F210(message);
        return;
    }
    if(stream == 6 && function == 207) //PanelInfoRequest
    {
        onReceiveS6F207(message);
        return;
    }
    if(stream == 2 && function == 208) //PanelInfoDownloadReply
    {
        onReceiveS2F208(message);
        return;
    }
    if(stream == 6 && function == 3) //EqpDiscreteVariableDataSendSites
    {
        onReceiveS6F3(message);
        return;
    }
    if(stream == 10 && function == 6) //TerminalDisplayMultiBlockAcknowledge
    {
        onReceiveS10F6(message);
        return;
    }
}
void B3HsmsHostSim::onReceiveS10F6(const HSMSMessages &message)
{
    qDebug() << "received S10F6 Message: TerminalDisplayMultiBlockAcknowledge";
    SECS2Item it = message.getItem();
    QString str = it.toASCII();
    qDebug() << "ACKC10 :" << str;
}

void B3HsmsHostSim::onReceiveS6F3(const HSMSMessages &message)
{
    qDebug() << "Build S6F4 Message";
    HSMSMessages rsp = HSMSMessages::replyDataMessage(message, "0");//S6,F4
    sendMessage(rsp);
}

void B3HsmsHostSim::onReceiveS6F11(const HSMSMessages &message)
{
    qDebug() << "Build S6F12 Message";
    m_OnLineLocal = true;
    HSMSMessages rsp = HSMSMessages::replyDataMessage(message, "0");//S6,F12 EventReportAcknowledge [D=0] [len=13] [SB=2]
    sendMessage(rsp);
}

void B3HsmsHostSim::onReceiveS2F208(const HSMSMessages &message)
{
    SECS2Item it = message.getItem();
    QString str = it.toASCII();
    qDebug() << "S2F208: PanelInfoDownloadReply ACK is " << str;
}

void B3HsmsHostSim::onReceiveS2F210(const HSMSMessages &message)
{
    SECS2Item it = message.getItem();
    QString str = it.toASCII();
    if(str == "0")
    {
        qDebug() << "S2F210 : OperatorAuthorityDownloadReply ACK is OK";
    }
    else
    {
        qDebug() << "S2F210 : OperatorAuthorityDownloadReply ACK is: " << str;
    }
}

void B3HsmsHostSim::onReceiveS6F207(const HSMSMessages &message)
{
    qDebug() << "Build S6F208 Message";
    HSMSMessages rsp = HSMSMessages::replyDataMessage(message, "0");//S6,F208 EventReportAcknowledge [D=0] [len=13] [SB=2]
    sendMessage(rsp);

    SECS2Item it = message.getItem();
    QVector<SECS2Item> list = it.toItems();

    QString EQPID = list[0].toASCII();
    QString UNITID = list[1].toASCII();
    QString PNLID = list[2].toASCII();
    QString MODULEID = list[3].toASCII();

    PANELINFOSET["EQPID"] = EQPID;
    PANELINFOSET["UNITID"] = UNITID;
    PANELINFOSET["PNLID"] = PNLID;
    PANELINFOSET["MODULEID"] = MODULEID;

    qDebug() << "S6F207: EQPID = " << EQPID << ", UNITID = " << UNITID << ", PNLID = " << PNLID << ", MODULEID = " << MODULEID;

    sendS2F207(PANELINFOSET);
}

void B3HsmsHostSim::onReceiveS1F6(const HSMSMessages &message)
{
    SECS2Item it = message.getItem();
    QVector<SECS2Item> list = it.toItems();
    QString SFCD = list[0].toASCII();
    QVector<SECS2Item> CREQSET = list[1].toItems();
    QString EQPID = CREQSET[0].toASCII();
    QString CRST = CREQSET[1].toASCII();
    QString EQST = CREQSET[2].toASCII();

    qDebug() << "S1F6: EQPID = " << EQPID << ", CRST = " << CRST << ", EQST = " << EQST;

}

void B3HsmsHostSim::onReceiveS6F209(const HSMSMessages &message)
{
    qDebug() << "Build S6F210 Message";
    HSMSMessages rsp = HSMSMessages::replyDataMessage(message, "0");//S6,F12 EventReportAcknowledge [D=0] [len=13] [SB=2]
    sendMessage(rsp);
    SECS2Item it = message.getItem();
    if(it.isList())
    {
        QVector<SECS2Item> list = it.toItems();
        QString operatorID = list[2].toASCII();
        QString password = list[3].toASCII();
        if(operatorID == "30145149" && password == "BOEHF")
        {
            HSMSMessages rsp = HSMSMessages::S2F209("3MMUV011", "", operatorID, "0");
            sendMessage(rsp);
            return;
        }
        HSMSMessages rsp = HSMSMessages::S2F209("3MMUV011", "", operatorID, "1");
        sendMessage(rsp);
        return;
    }   
    return;
}

void B3HsmsHostSim::sendS1F5()
{
    sendS1F5("1");
}

void B3HsmsHostSim::sendS1F5(QString str)
{
    SECS2Item it;
    it.setASCII(str);
    HSMSMessages m = HSMSMessages::dataMessage(0, 1, 5, 1, it);
    m.setWBit(HSMSMessages::NeedReply);
    sendMessage(m);
}

void B3HsmsHostSim::sendS2F207(QMap<QString, QString> PANELINFOSET)
{
    SECS2Item its[17];
    if(PANELINFOSET["PNLID"] == "3AUE090016A4BM")
    {
        PANELINFOSET["RTCODE"] = "0";
        PANELINFOSET["FGCODE"] = "TV080WUM-AS0-38P0";
        PANELINFOSET["MODULEID"] = "3AUE090016A4BM-11";
    }
    else {
        PANELINFOSET["RTCODE"] = "1";
        PANELINFOSET["FGCODE"] = "";
    }
    its[0].setASCII(PANELINFOSET["RTCODE"]);
    its[1].setASCII(PANELINFOSET["EQPID"]);
    its[2].setASCII("3MMCB081-CB11,3MMFB081-FB11,3MMPB111-FB21,3MMTF041-TF22");
    its[3].setASCII(PANELINFOSET["PNLID"]);
    its[4].setASCII(PANELINFOSET["MODULEID"]);
    its[5].setASCII(PANELINFOSET["FGCODE"]);
    its[6].setASCII(PANELINFOSET["WORKORDER"]);
    its[7].setASCII(PANELINFOSET["PNLJUDGE"]);
    its[8].setASCII(PANELINFOSET["PNLGRADE"]);
    its[9].setASCII(PANELINFOSET["REASONCODE"]);
    its[10].setASCII(PANELINFOSET["CURRENTSTEP"]);
    its[11].setASCII(PANELINFOSET["GROUPID"]);
    its[12].setASCII(PANELINFOSET["INSPFILENAME"]);
    its[13].setASCII(PANELINFOSET["INSPFILEPATH"]);
    its[14].setASCII(PANELINFOSET["REVISION"]);
    its[15].setASCII(PANELINFOSET["CELLGRADE"]);
    its[16].setASCII(PANELINFOSET["WOGRADE"]);

    SECS2Item it;
    it.setItems(its, 17);

    HSMSMessages rsp = HSMSMessages::dataMessage(0, 2, 207, 0, it);
    sendMessage(rsp);
}
